package com.book.publishing.controller;

import com.book.buyout.entity.Vender;
import com.book.buyout.service.VenderService;
import com.book.common.annotation.ControllerEndpoint;
import com.book.publishing.entity.Bookman;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.FebsUtil;
import com.book.publishing.service.BookmanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: 孙鑫
 * @Description:  20173952
 */

@Controller
public class BookmanController extends BaseController {

    @Autowired
    BookmanService bookmanService;
    @Autowired
    VenderService venderService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "publishing/bookman")
    public String bookmanIndex() {
        return FebsUtil.view("publishing/bookman/bookmanList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "publishing/bookman/add")
    public String bookmanAdd() {
        return FebsUtil.view("publishing/bookman/bookmanAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "publishing/bookman/update/{id}")
    public String bookmanUpdate(@PathVariable Integer id, ModelMap modelMap) {
        Bookman bookman = this.bookmanService.getById(id);
        modelMap.put("bookman", bookman);
        return FebsUtil.view("publishing/bookman/bookmanUpdate");
    }

    @GetMapping("bookman")
    @ResponseBody
    @RequiresPermissions("bookman:view")
    public FebsResponse bookmanList(Bookman bookman) {
        return new FebsResponse().success().data(this.bookmanService.findBookman(bookman));
    }

    @Log("访问出版社信息")
    @GetMapping("bookman/list")
    @ResponseBody
    @RequiresPermissions("bookman:view")
    public FebsResponse bookmanIPage(QueryRequest request, Bookman bookman) {
        Map<String, Object> dataTable = getDataTable(this.bookmanService.pageBookman(request, bookman));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("出版社-新增")
    @PostMapping("bookman")
    @ResponseBody
    @RequiresPermissions("bookman:add")
    @ControllerEndpoint(operation = "新增出版社信息", exceptionMessage = "新增出版社信息失败")
    public FebsResponse bookmanAdd(Bookman bookman) {
        List<Bookman> bookmans = bookmanService.findBookman(bookman);
        if (bookmans.size() == 0) {
            bookman.setStatus("0");
            bookmanService.addBookman(bookman);
            return new FebsResponse().success();
        }
        return new FebsResponse().message("该出版社信息已经存在！");
    }

    @Log("出版社-修改")
    @PostMapping("bookman/update")
    @ResponseBody
    @RequiresPermissions("bookman:update")
    @ControllerEndpoint(operation = "修改出版社信息成功", exceptionMessage = "修改出版社信息失败")
    public FebsResponse bookmanUpdate(Bookman bookman) {
        bookmanService.updateBookman(bookman);
        return new FebsResponse().success();
    }

    @Log("出版社-删除")
    @PostMapping("bookman/delete/{ids}")
    @ResponseBody
    @RequiresPermissions("bookman:delete")
    @ControllerEndpoint(operation = "删除出版社信息成功", exceptionMessage = "删除出版社信息失败")
    public FebsResponse bookmandelete(@PathVariable String ids) {
        String[] attr = ids.split(",");
        List<Bookman> bookmanList = bookmanService.getBookmanByIds(attr);
        for (int i = 0; i < bookmanList.size(); i++) {
            Vender vender = venderService.findByName(bookmanList.get(i).getPubname());
            if (vender != null) {
                venderService.removeById(vender.getId());
            }
        }
        this.bookmanService.deleteBookman(attr);
        return new FebsResponse().success();
    }
}